package gui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;

import java.awt.Color;

import javax.swing.border.LineBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.DefaultComboBoxModel;

import classes_basicas.Cliente;
import classes_basicas.Endereco;
import fachada.Fachada;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class Alterar_Cliente {

	JFrame frame;
	private JTextField txbnome;
	private JTextField txbcpf;
	private JTextField txbtelefone;
	private JTextField txblogradouro;
	private JTextField txbbairro;
	private JTextField txbcidade;
	private JTable table;
	Cliente j = new Cliente();
	Cliente c = new Cliente();
	Endereco e = new Endereco();
	private JTextField txbfiltronome;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Alterar_Cliente window = new Alterar_Cliente();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Alterar_Cliente() {
		initialize();
		try {
			listando2(c);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
public boolean listando(Cliente cliente) throws Exception{
	try{
	Fachada f = new Fachada();
	
	ArrayList<Cliente> listc = new ArrayList<>();
	listc= (ArrayList<Cliente>) f.listarcliente(cliente);
	
	DefaultTableModel table2 = new DefaultTableModel();
	table2.getDataVector().removeAllElements();
	table2.setColumnIdentifiers(new String[]{"Id_Cliente", "Nome", "Cpf","Sexo","Telefone","Lougradouro","Bairro","Cidade","Estado" });
    for (int i = 0; i < listc.size();
     i++) { table2.addRow(new
     String[]{(Long.toString(listc.get(i).getId())), listc.get(i).getNome(),listc.get(i).getCpf(),listc.get(i).getSexo(),listc.get(i).getTelefone(),listc.get(i).getEndereco().getLogradouro(),
    		 listc.get(i).getEndereco().getBairro(),listc.get(i).getEndereco().getCidade(),listc.get(i).getEndereco().getEstado()
     });
    
     }
     table.setModel(table2);
	return true;
	}catch(Exception e){return false;}
	
}
public void listando2(Cliente cliente) throws Exception{
	
	Fachada f = new Fachada();
	cliente.setNome("");
	ArrayList<Cliente> listc = new ArrayList<>();
	listc= (ArrayList<Cliente>) f.listarcliente(cliente);
	
	DefaultTableModel table2 = new DefaultTableModel();
	table2.getDataVector().removeAllElements();
	table2.setColumnIdentifiers(new String[]{"Id_Cliente", "Nome", "Cpf","Sexo","Telefone","Lougradouro","Bairro","Cidade","Estado" });
    for (int i = 0; i < listc.size();
     i++) { table2.addRow(new
     String[]{(Long.toString(listc.get(i).getId())), listc.get(i).getNome(),listc.get(i).getCpf(),listc.get(i).getSexo(),listc.get(i).getTelefone(),listc.get(i).getEndereco().getLogradouro(),
    		 listc.get(i).getEndereco().getBairro(),listc.get(i).getEndereco().getCidade(),listc.get(i).getEndereco().getEstado()
     });
    
     }
     table.setModel(table2);
	
	
}


	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 837, 475);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setForeground(new Color(128, 0, 0));
		panel.setLayout(null);
		panel.setBorder(new LineBorder(new Color(128, 0, 0), 2));
		panel.setBackground(new Color(135, 206, 250));
		panel.setBounds(0, 0, 829, 445);
		frame.getContentPane().add(panel);
		
		JLabel lblAlterao = new JLabel("Altera\u00E7\u00E3o de Clientes");
		lblAlterao.setForeground(new Color(128, 0, 0));
		lblAlterao.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblAlterao.setBounds(281, 42, 196, 25);
		panel.add(lblAlterao);
		
		JLabel label_1 = new JLabel("Nome:");
		label_1.setForeground(new Color(128, 0, 0));
		label_1.setBounds(59, 107, 46, 14);
		panel.add(label_1);
		
		JLabel label_2 = new JLabel("Cpf:");
		label_2.setForeground(new Color(128, 0, 0));
		label_2.setBounds(59, 132, 46, 14);
		panel.add(label_2);
		
		JLabel label_3 = new JLabel("Sexo:");
		label_3.setForeground(new Color(128, 0, 0));
		label_3.setBounds(59, 157, 46, 14);
		panel.add(label_3);
		
		JLabel label_4 = new JLabel("Telefone:");
		label_4.setForeground(new Color(128, 0, 0));
		label_4.setBounds(59, 183, 62, 14);
		panel.add(label_4);
		
		txbnome = new JTextField();
		txbnome.setForeground(new Color(128, 0, 0));
		txbnome.setColumns(10);
		txbnome.setBounds(97, 104, 246, 20);
		panel.add(txbnome);
		
		txbcpf = new JTextField();
		txbcpf.setForeground(new Color(128, 0, 0));
		txbcpf.setColumns(10);
		txbcpf.setBounds(97, 132, 246, 20);
		panel.add(txbcpf);
		
		txbtelefone = new JTextField();
		txbtelefone.setForeground(new Color(128, 0, 0));
		txbtelefone.setColumns(10);
		txbtelefone.setBounds(118, 180, 225, 20);
		panel.add(txbtelefone);
		
		final JComboBox combosexo = new JComboBox();
		combosexo.setForeground(new Color(128, 0, 0));
		combosexo.setModel(new DefaultComboBoxModel(new String[] {"Selecione o sexo", "Masculino", "Feminino"}));
		combosexo.setBounds(97, 157, 144, 20);
		panel.add(combosexo);
		
		JLabel label_5 = new JLabel("Logradouro:");
		label_5.setForeground(new Color(128, 0, 0));
		label_5.setBounds(59, 208, 73, 14);
		panel.add(label_5);
		
		txblogradouro = new JTextField();
		txblogradouro.setForeground(new Color(128, 0, 0));
		txblogradouro.setColumns(10);
		txblogradouro.setBounds(128, 205, 215, 20);
		panel.add(txblogradouro);
		
		JLabel label_6 = new JLabel("Bairro:");
		label_6.setForeground(new Color(128, 0, 0));
		label_6.setBounds(59, 234, 62, 14);
		panel.add(label_6);
		
		txbbairro = new JTextField();
		txbbairro.setForeground(new Color(128, 0, 0));
		txbbairro.setColumns(10);
		txbbairro.setBounds(97, 231, 246, 20);
		panel.add(txbbairro);
		
		JLabel label_7 = new JLabel("Cidade:");
		label_7.setForeground(new Color(128, 0, 0));
		label_7.setBounds(59, 262, 62, 14);
		panel.add(label_7);
		
		txbcidade = new JTextField();
		txbcidade.setForeground(new Color(128, 0, 0));
		txbcidade.setColumns(10);
		txbcidade.setBounds(118, 259, 225, 20);
		panel.add(txbcidade);
		
		JLabel label_8 = new JLabel("Estado:");
		label_8.setForeground(new Color(128, 0, 0));
		label_8.setBounds(59, 290, 62, 14);
		panel.add(label_8);
		
		final JComboBox comboestado = new JComboBox();
		comboestado.setModel(new DefaultComboBoxModel(new String[] {"Selecione o estado:", "Acre ", "Alagoas ", "Amap\u00E1 ", "Amazonas ", "Bahia  ", "Cear\u00E1 ", "Distrito Federal  ", "Esp\u00EDrito Santo ", "Goi\u00E1s", "Maranh\u00E3o ", "Mato Grosso ", "Mato Grosso do Sul ", "Minas Gerais ", "Par\u00E1 ", "Para\u00EDba ", "Paran\u00E1 ", "Pernambuco ", "Piau\u00ED ", "Rio de Janeiro ", "Rio Grande do Norte ", "Rio Grande do Sul ", "Rond\u00F4nia ", "Roraima ", "Santa Catarina ", "S\u00E3o Paulo ", "Sergipe ", "Tocantins "}));
		comboestado.setBounds(112, 287, 144, 20);
		panel.add(comboestado);
		
		JButton button = new JButton("Salvar");
		button.setForeground(new Color(128, 0, 0));
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				try {
					if(table.getSelectedRow()<0){
						
						JOptionPane.showMessageDialog(null, "Selecione o cliente a ser alterado");
						return;
					}
					c.setNome(txbnome.getText());
					c.setCpf(txbcpf.getText());
					c.setSexo((String) combosexo.getSelectedItem());
					c.setTelefone(txbtelefone.getText());
					e.setLogradouro(txblogradouro.getText());
					e.setBairro(txbbairro.getText());
					e.setCidade(txbcidade.getText());
					e.setEstado((String) comboestado.getSelectedItem());
					c.setEndereco(e);
					
					Fachada f = new Fachada();
					
						f.alterarcliente(c);
						JOptionPane.showMessageDialog(null,"Alterado com sucesso!");
					
				} catch (Exception e1) {
					JOptionPane.showMessageDialog(null,e1.getMessage());
				}
			}
		});
		button.setBounds(122, 340, 90, 28);
		panel.add(button);
		
		JButton button_1 = new JButton("Voltar");
		button_1.setForeground(new Color(128, 0, 0));
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				Menu m = new Menu();
				frame.setVisible(false);
				m.frame.show();
			}
		});
		button_1.setBounds(224, 340, 90, 28);
		panel.add(button_1);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				
			}
		});
		scrollPane.setBounds(353, 116, 466, 252);
		panel.add(scrollPane);
		
		table = new JTable();
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				int i =table.getSelectedRow();
				String k = (table.getValueAt(i, 0).toString());
				c.setId(Long.parseLong(k));
				txbnome.setText(table.getValueAt(i, 1).toString());
				txbcpf.setText(table.getValueAt(i, 2).toString());
				combosexo.setSelectedItem((table.getValueAt(i, 3).toString()));
				txbtelefone.setText(table.getValueAt(i, 4).toString());
				txblogradouro.setText(table.getValueAt(i, 5).toString());
				txbbairro.setText(table.getValueAt(i, 6).toString());
				txbcidade.setText(table.getValueAt(i, 7).toString());
				comboestado.setSelectedItem((table.getValueAt(i, 8).toString()));
			}
		});
		table.setModel(new DefaultTableModel(
			new Object[][] {
			},
			new String[] {
				"Id_Cliente", "Nome", "Cpf", "Sexo", "Telefone", "Logradouro", "Bairro", "Cidade", "Estado"
			}
		));
		scrollPane.setViewportView(table);
		
		txbfiltronome = new JTextField();
		txbfiltronome.setBounds(503, 83, 122, 28);
		panel.add(txbfiltronome);
		txbfiltronome.setColumns(10);
		
		JButton btnSalvarbuscar = new JButton("Buscar");
		btnSalvarbuscar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				Cliente h = new Cliente();
				h.setNome(txbfiltronome.getText());
				try {
					listando(h);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					JOptionPane.showMessageDialog(null,e.getMessage());
				}
			}
		});
		btnSalvarbuscar.setForeground(new Color(128, 0, 0));
		btnSalvarbuscar.setBounds(637, 83, 90, 28);
		panel.add(btnSalvarbuscar);
		
		JLabel lblPesquisarPorNome = new JLabel("Pesquisar por nome:");
		lblPesquisarPorNome.setForeground(new Color(128, 0, 0));
		lblPesquisarPorNome.setBounds(369, 90, 122, 14);
		panel.add(lblPesquisarPorNome);
	}
}
